package personal.vivian.filter;


import java.util.Enumeration;
import java.util.Map;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class ModifyParameterWrapper extends HttpServletRequestWrapper {
    private Map<String,String[]> parameterMap;
    public ModifyParameterWrapper(HttpServletRequest request) {
        super(request);
        parameterMap= request.getParameterMap();
    }

    @Override
    public String getParameter(String name) {
        String[] res = parameterMap.get(name);
        if(res==null||res.length<=0){
            return null;
        }else {
            System.out.println("修改之前："+res[0]);
            return modify(res[0]);
        }
    }

    @Override
    public String[] getParameterValues(String name) {
        String[] res = parameterMap.get(name);
        if(res==null||res.length<=0){
            return null;
        }else {
            for(int i=0;i<res.length;i++){
                System.out.println("修改之前(实际调用)："+res[i]);
                res[i]=modify(res[i]);
            }
        }
        return res;
    }

    @Override
    public Enumeration<String> getParameterNames() {
        Vector<String> vector = new Vector<>(parameterMap.keySet());
        return vector.elements();
    }

    private String modify(String s){
        return "Modified:"+s;
    }
}
